/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.data;

import com.tlf.dyjc5.service.RsjService;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.dusin.web.util.CommonWebUtil;
import org.dusin.web.util.JsonUtil;
import org.dusin.web.util.TheUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Administrator
 */
public class RsjSave extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            RsjService rsjService = (RsjService) context.getBean("rsjService");
            String msg = null;
            String currentOrgCode = (String) request.getSession().getAttribute("currentOrgCode");
            String command = request.getParameter("cmd");
            String lock = request.getParameter("lock");
            boolean result = false;

            String idStr = request.getParameter("id");
            long id = CommonWebUtil.getLongParameter(idStr, 0);;

            String deviceIdStr = request.getParameter("deviceId");
            long deviceId = CommonWebUtil.getLongParameter(deviceIdStr, 0);

            String pointIdStr = request.getParameter("pointId");
            long pointId = CommonWebUtil.getLongParameter(pointIdStr, 0);

            String defaultTotalTimeStr = request.getParameter("defaultTotalTime");
            long defaultTotalTime = CommonWebUtil.getLongParameter(defaultTotalTimeStr, 0);

            String aboveUpperLimitNumberStr = request.getParameter("aboveUpperLimitNumber");
            int aboveUpperLimitNumber = CommonWebUtil.getIntParameter(aboveUpperLimitNumberStr, 0);

            String underLowerLimitNumberStr = request.getParameter("underLowerLimitNumber");
            int underLowerLimitNumber = CommonWebUtil.getIntParameter(underLowerLimitNumberStr, 0);

            String dataYearStr = request.getParameter("dataYear");
            int dataYear = CommonWebUtil.getIntParameter(dataYearStr, 0);

            String dataMonthStr = request.getParameter("dataMonth");
            int dataMonth = CommonWebUtil.getIntParameter(dataMonthStr, 0);

            String dataDayStr = request.getParameter("dataDay");
            int dataDay = CommonWebUtil.getIntParameter(dataDayStr, 0);

            String typeStr = request.getParameter("type");
            long type = CommonWebUtil.getLongParameter(typeStr, 0);

            String totalRunMinStr = request.getParameter("totalRunMin");
            long totalRunMin = CommonWebUtil.getLongParameter(totalRunMinStr, 1);

            String aboveUpperLimitMinStr = request.getParameter("aboveUpperLimitMin");
            long aboveUpperLimitMin = CommonWebUtil.getLongParameter(aboveUpperLimitMinStr, 0);

            String underLowerLimitMinStr = request.getParameter("underLowerLimitMin");
            long underLowerLimitMin = CommonWebUtil.getLongParameter(underLowerLimitMinStr, 0);

            String maxVoltageStr = request.getParameter("maxVoltage");
            double maxVoltage = CommonWebUtil.getDoubleParameter(maxVoltageStr, 0);

            String minVoltageStr = request.getParameter("minVoltage");
            double minVoltage = CommonWebUtil.getDoubleParameter(minVoltageStr, 0);

            String avgVoltageStr = request.getParameter("avgVoltage");
            double avgVoltage = CommonWebUtil.getDoubleParameter(avgVoltageStr, 0);

            String h00Str = request.getParameter("h00");
            long h00 = CommonWebUtil.getLongParameter(h00Str, 0);

            String h01Str = request.getParameter("h01");
            long h01 = CommonWebUtil.getLongParameter(h01Str, 0);

            String h02Str = request.getParameter("h02");
            long h02 = CommonWebUtil.getLongParameter(h02Str, 0);

            String h03Str = request.getParameter("h03");
            long h03 = CommonWebUtil.getLongParameter(h03Str, 0);

            String h04Str = request.getParameter("h04");
            long h04 = CommonWebUtil.getLongParameter(h04Str, 0);

            String h05Str = request.getParameter("h05");
            long h05 = CommonWebUtil.getLongParameter(h05Str, 0);

            String h06Str = request.getParameter("h06");
            long h06 = CommonWebUtil.getLongParameter(h06Str, 0);

            String h07Str = request.getParameter("h07");
            long h07 = CommonWebUtil.getLongParameter(h07Str, 0);

            String h08Str = request.getParameter("h08");
            long h08 = CommonWebUtil.getLongParameter(h08Str, 0);

            String h09Str = request.getParameter("h09");
            long h09 = CommonWebUtil.getLongParameter(h09Str, 0);

            String h10Str = request.getParameter("h10");
            long h10 = CommonWebUtil.getLongParameter(h10Str, 0);

            String h11Str = request.getParameter("h11");
            long h11 = CommonWebUtil.getLongParameter(h11Str, 0);

            String h12Str = request.getParameter("h12");
            long h12 = CommonWebUtil.getLongParameter(h12Str, 0);

            String h13Str = request.getParameter("h13");
            long h13 = CommonWebUtil.getLongParameter(h13Str, 0);

            String h14Str = request.getParameter("h14");
            long h14 = CommonWebUtil.getLongParameter(h14Str, 0);

            String h15Str = request.getParameter("h15");
            long h15 = CommonWebUtil.getLongParameter(h15Str, 0);

            String h16Str = request.getParameter("h16");
            long h16 = CommonWebUtil.getLongParameter(h16Str, 0);

            String h17Str = request.getParameter("h17");
            long h17 = CommonWebUtil.getLongParameter(h17Str, 0);

            String h18Str = request.getParameter("h18");
            long h18 = CommonWebUtil.getLongParameter(h18Str, 0);

            String h19Str = request.getParameter("h19");
            long h19 = CommonWebUtil.getLongParameter(h19Str, 0);

            String h20Str = request.getParameter("h20");
            long h20 = CommonWebUtil.getLongParameter(h20Str, 0);

            String h21Str = request.getParameter("h21");
            long h21 = CommonWebUtil.getLongParameter(h21Str, 0);

            String h22Str = request.getParameter("h22");
            long h22 = CommonWebUtil.getLongParameter(h22Str, 0);

            String h23Str = request.getParameter("h23");
            long h23 = CommonWebUtil.getLongParameter(h23Str, 0);

            String statusStr = request.getParameter("status");
            int status = CommonWebUtil.getIntParameter(statusStr, 0);

            String reasonAnalyze = request.getParameter("reasonAnalyze");

            String treatWay = request.getParameter("treatWay");

            String completeCase = request.getParameter("completeCase");

            String dataDateStr = request.getParameter("dataDate");
            Timestamp dataDate = CommonWebUtil.getTimestampParamter(dataDateStr);

            String maxVoltageTimeStr = request.getParameter("maxVoltageTime");
            maxVoltageTimeStr = dataYearStr + "-" + dataMonthStr + "-" + dataDayStr + " " + maxVoltageTimeStr + ".0";
            System.out.println(maxVoltageTimeStr);
            Timestamp maxVoltageTime = CommonWebUtil.getTimestampParamter(maxVoltageTimeStr);

            String minVoltageTimeStr = request.getParameter("minVoltageTime");
            minVoltageTimeStr = dataYearStr + "-" + dataMonthStr + "-" + dataDayStr + " " + minVoltageTimeStr + ".0";
            Timestamp minVoltageTime = CommonWebUtil.getTimestampParamter(minVoltageTimeStr);

            if ("create".equals(command)) {
                result = rsjService.add(deviceId, pointId, defaultTotalTime, aboveUpperLimitNumber, underLowerLimitNumber, dataYear, dataMonth, dataDay, dataDate, type, totalRunMin, aboveUpperLimitMin,
                        underLowerLimitMin, maxVoltage, maxVoltageTime, minVoltage, minVoltageTime, avgVoltage, h00, h01, h02, h03, h04, h05, h06, h07, h08, h09, h10, h11, h12, h13, h14, h15,
                        h16, h17, h18, h19, h20, h21, h22, h23, status, reasonAnalyze, treatWay, completeCase);
            } else if ("createLock".equals(command)) {
                String orgCode = request.getParameter("orgCode");
                String company = request.getParameter("company");
                String subCompany = request.getParameter("subCompany");
                String station = request.getParameter("station");
                String name = request.getParameter("name");
                String pointArea = request.getParameter("pointArea");
                String pointPosition = request.getParameter("pointPosition");
                int pointTypeId = CommonWebUtil.getIntParameter(request.getParameter("pointTypeId"), 0);
                String pointType = request.getParameter("pointType");
                int pointType2Id = CommonWebUtil.getIntParameter(request.getParameter("pointType2Id"), 0);
                String pointType2 = request.getParameter("pointType2");
                double lowerLimit = CommonWebUtil.getDoubleParameter(request.getParameter("lowerLimit"), 0);
                double upperLimit = CommonWebUtil.getDoubleParameter(request.getParameter("upperLimit"), 0);

                int startStatus = CommonWebUtil.getIntParameter(request.getParameter("startStatus"), 0);
                int reportStatus = CommonWebUtil.getIntParameter(request.getParameter("reportStatus"), 0);
                int pointStatus = reportStatus * 10 + startStatus;

                int orgTypeId = CommonWebUtil.getIntParameter(request.getParameter("orgTypeId"), 0);
                String orgTypeName = request.getParameter("orgTypeName");
                double voltage = CommonWebUtil.getDoubleParameter(request.getParameter("voltage"), 0);
                double voltageLevelValue = CommonWebUtil.getDoubleParameter(request.getParameter("voltageLevelValue"), 0);
                int voltageLevelId = CommonWebUtil.getIntParameter(request.getParameter("voltageLevelId"), 0);
                String voltageLevelName = request.getParameter("voltageLevelName");
                String pointCode = request.getParameter("pointCode");
                String companyCode=request.getParameter("companyCode");
                result = rsjService.addLock(deviceId, pointId, defaultTotalTime, aboveUpperLimitNumber, underLowerLimitNumber, dataYear, dataMonth, dataDay, dataDate, type, totalRunMin, aboveUpperLimitMin,
                        underLowerLimitMin, maxVoltage, maxVoltageTime, minVoltage, minVoltageTime, avgVoltage, h00, h01, h02, h03, h04, h05, h06, h07, h08, h09, h10, h11, h12, h13, h14, h15,
                        h16, h17, h18, h19, h20, h21, h22, h23, status, orgCode, company, subCompany, station, name, pointArea, pointPosition, pointTypeId, pointType, pointType2Id, pointType2, lowerLimit,
                        upperLimit, pointStatus, orgTypeId, orgTypeName, voltage, voltageLevelValue, voltageLevelId, voltageLevelName,pointCode,companyCode);
                
            } else if ("update".equals(command)) {
                result = rsjService.update(id, defaultTotalTime, aboveUpperLimitNumber, underLowerLimitNumber, dataYear, dataMonth, dataDay, dataDate, type, totalRunMin, aboveUpperLimitMin,
                        underLowerLimitMin, maxVoltage, maxVoltageTime, minVoltage, minVoltageTime, avgVoltage, h00, h01, h02, h03, h04, h05, h06, h07, h08, h09, h10, h11, h12, h13, h14,
                        h15, h16, h17, h18, h19, h20, h21, h22, h23, status, reasonAnalyze, treatWay, completeCase);
            } else if ("updateLock".equals(command)) {
                result = rsjService.updateLockByKey(defaultTotalTime, aboveUpperLimitNumber, underLowerLimitNumber, dataYear, dataMonth, dataDay, dataDate, type, totalRunMin, aboveUpperLimitMin,
                        underLowerLimitMin, maxVoltage, maxVoltageTime, minVoltage, minVoltageTime, avgVoltage, h00, h01, h02, h03, h04, h05, h06, h07, h08, h09, h10, h11, h12, h13, h14,
                        h15, h16, h17, h18, h19, h20, h21, h22, h23, status, pointId);

            } else if ("reason".equals(command)) {
                result = rsjService.updateReason(id, reasonAnalyze, treatWay, completeCase);
            } else if ("batchCreate".equals(command)) {
                String date = request.getParameter("dateTo");
                String condition = TheUtil.getPointSelectCondition(request);

                //String rateStr = request.getParameter("rate");
                String rateStr = "0.95";
                //sb.append(" and startStatus=1");                
                result = rsjService.trBatchCreate(currentOrgCode, date, condition, Double.parseDouble(rateStr), lock);
            } else if("dRateSet".equals(command)){
                String date = request.getParameter("dateTo");
                String condition = TheUtil.getPointSelectCondition(request);
                String rateStr = request.getParameter("rate");
                //sb.append(" and startStatus=1");

                result = rsjService.updateAllByRateV2(currentOrgCode, date, condition, Double.parseDouble(rateStr));
            }else if ("remove".equals(command)) {

                result = rsjService.deleteByKey(pointId, dataYear, dataMonth, dataDay, lock);

            } else if ("removeAll".equals(command)) {

                String date = request.getParameter("dateTo");
                String condition = TheUtil.getPointSelectCondition(request);
                result = rsjService.trDeleteAll(currentOrgCode, date, date, condition, lock);

            }else if("recallSave".equals(command)){ //召回数据保存
                result = rsjService.deleteByKey(pointId, dataYear, dataMonth, dataDay, "");
                result = rsjService.add(deviceId, pointId, defaultTotalTime, aboveUpperLimitNumber, underLowerLimitNumber, dataYear, dataMonth, dataDay, dataDate, type, totalRunMin, aboveUpperLimitMin,
                        underLowerLimitMin, maxVoltage, maxVoltageTime, minVoltage, minVoltageTime, avgVoltage, h00, h01, h02, h03, h04, h05, h06, h07, h08, h09, h10, h11, h12, h13, h14, h15,
                        h16, h17, h18, h19, h20, h21, h22, h23, status, reasonAnalyze, treatWay, completeCase);
                //System.out.println(maxVoltageTime);
            }

            if (result) {
                out.println(JsonUtil.createSuccessJsonResponse(msg));
            } else {
                out.print(JsonUtil.createFailureJsonResponse("数据库操作失败"));
            }

        } catch (DuplicateKeyException e) {
            String msg = e.getMessage();
            if (msg.indexOf("NAME_t_voltageLevel") != -1) {
                out.print(JsonUtil.createFailureJsonResponse("名称不能和现有数据库中的记录重复"));
            }
        } catch (Exception e) {
            out.print(JsonUtil.createFailureJsonResponse("不明操作错误"));
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
